<?php
// +----------------------------------------------------------------------
// | A3Mall
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: xzncit <158373108@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller\job;

use app\admin\controller\Auth;
use app\common\model\goods\GoodsAttribute;
use app\common\model\goods\GoodsExtends;
use app\common\model\goods\GoodsItem;
use app\common\model\goods\GoodsModel;
use app\common\model\users\WalletDetail;
use mall\basic\Attachments;
use mall\utils\Data;
use mall\utils\Date;
use mall\response\Response;
use mall\utils\Tool;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;
use app\common\model\job\Job;
use app\common\model\job\JobType;
use app\common\model\users\Users as UsersModel;

class Index extends Auth {

    public function index(){
        if(Request::isAjax()){
            $limit = Request::get("limit");
            $params = Request::param("key/a",[],"trim,strip_tags");
            $condition = [];
            if(!empty($params['publish_time'])){
                [$start_time,$end_time]=explode('~',$params['publish_time']);
                $condition[]=['publish_time','>=',strtotime($start_time)];
                $condition[]=['publish_time','<=',strtotime($end_time)];
            }
            if(!empty($params['exp_time'])){
                [$start_time,$end_time]=explode('~',$params['exp_time']);
                $condition[]=['exp_time','>=',strtotime($start_time)];
                $condition[]=['exp_time','<=',strtotime($end_time)];
            }
            if(!empty($params['type_id'])){
                $condition[]=['type_id','=',$params['type_id']];
            }
            if(!empty($params['publisher'])){
                $condition[]=['users.username','like',"%{$params['publisher']}%"];
            }
            if(isset($params['status']) && $params['status']!=-1){
                $condition[]=['job.status','=',$params['status']];
            }
            $job = new Job();
            $list = $job->getList($condition,$limit);

            $list['data'] = array_map(function($value){
                $value = $value->toArray();
                $value = array_merge($value,[
                    'accept_issue'  => $value['accept_num'] . ' / ' . $value['num'],
                    'finish_fail'  => $value['accept_finish_num'] . ' / ' . $value['accept_fail_num'],
                    'price_total'  => ($value['price'] - 0) . ' / ' . ($value['total_amount'] -0),
                    'requirement'  => $value['requirement']['requirement'],
                    'publish_time_text' => date('y/m/d H:i',strtotime($value['publish_time_text'])),
                    'exp_time_text' => date('y/m/d H:i',strtotime($value['exp_time_text'])),
                ]);
                return $value;
            },$list['data']);

            if(empty($list['data'])){
                return Response::returnArray("当前还没有数据哦！",1);
            }
            return Response::returnArray("ok",0,$list['data'],$list['count']);
        }
        return View::fetch("",[
            "type_list"=>Data::analysisTree(Data::familyProcess(JobType::getAvailableTypeList())),
            'status_list'=>Job::getStatusList(),
        ]);
    }

    /**
     * 获取详情
     * @return string
     */
    public function details(){

        if($jobDetails = (new Job())->findById(Request::get('id',0,'intval'))) {
            $jobDetails->requirement;
            $jobDetails->publisher;
            $jobDetails->images;
         //   $jobDetails->jobType;
            $jobDetails = $jobDetails->toArray();
            // 获取类型
            $jobDetails['job_type'] = (new JobType())->find($jobDetails['type_id'])->toArray();
            $jobDetails['images'] = array_map(function($image){
                return [
                    'thumb' => Tool::url($image['filename'],null,'job'),
                    'url'   => Tool::url($image['filename']),
                ];
            },$jobDetails['images'] );
            $jobDetails['statusText'] = Job::getStatusText($jobDetails['status']);
            $jobDetails['payment'] = (new WalletDetail())->where('related_pid','=',$jobDetails['id'])->where('type','=',WalletDetail::TYPE_LIST['acceptIn'][0])->sum('amount');
        }
        return View::fetch("",[
            'jobDetails'    => $jobDetails,
        ]);
    }

    //任务审核
    public function audit(){
        if(Request::isAjax()){
            $id = Request::param('id',0,'intval');
            $status = Request::param('status',0,'intval');
            if(!$id){
                return Response::returnArray("非法操作",1);
            }
            if($status!=Job::STATUS_EFFECTIVE){
                return Response::returnArray("无效参数",1);
            }
            $job=Job::find($id);
            if(!$job){
                return Response::returnArray("记录不存在！",1);
            }
            if($job['status']!=Job::STATUS_PENDING_APPROVAL ){
                return Response::returnArray("无效操作！",1);
            }
            try {
                Job::where('id',$id)->update(['status'=>$status,'update_time'=>time()]);
                return Response::returnArray("操作成功",0);
            } catch (\Throwable $t) {
                return Response::returnArray($t->getMessage(),$t->getCode());
            }
        }
        return View::fetch("");
    }

}
